package com.cslg.ac.demo.service.user;

import com.cslg.ac.demo.constant.ResponseCode;
import com.cslg.ac.demo.dao.user.person.PersonInfoDAO;
import com.cslg.ac.demo.dao.user.UserDAO;
import com.cslg.ac.demo.dao.user.po.person.PersonInfoPO;
import com.cslg.ac.demo.dao.user.po.UserPO;
import com.cslg.ac.demo.entity.user.person.PersonInfo;
import com.cslg.ac.demo.util.string.StringUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Service;

@Service
@PropertySource(value = {"classpath:user.properties"},encoding="utf-8")
public class PersonInfoService {
    @Autowired
    PersonInfoDAO personInfoDAO;
    @Autowired
    UserDAO userDAO;
    @Value("${personBuket}")
    String personBuket;


    public PersonInfoPO getInfo(int id){
        PersonInfoPO personInfoPO = personInfoDAO.selectById(id);
        if(personInfoPO == null){
            return null;
        }
        personInfoPO.setImage(null);
        personInfoPO.setIdCard(null);
        personInfoPO.setRealName(null);
        return personInfoPO;
    }

    /**
     * 增加爱心值
     * @param id
     * @return
     */
    public int addLovevalue(Integer id,Integer count){

        PersonInfoPO personInfoPO = personInfoDAO.selectById(id);
        if(personInfoPO == null){
            return ResponseCode.USER_NOT_EXISTS;
        }
        int lovevalue = personInfoPO.getLoveValue();
        personInfoPO.setLoveValue(lovevalue+count);
        personInfoDAO.updateAllById(personInfoPO);
        return ResponseCode.REQUEST_SUCCESSED;
    }


    /**
     * 设置基本及资料
     * @return
     */
    public int simpleInfo(PersonInfo personInfo){
        PersonInfoPO personInfoPO = new PersonInfoPO();
        BeanUtils.copyProperties(personInfo,personInfoPO);
        UserPO userPO = userDAO.SelectById(personInfo.getId());
        if(userPO == null){
            return ResponseCode.USER_NOT_EXISTS;
        }
        PersonInfoPO personInfoPO1 = personInfoDAO.selectByPhone(userPO.getUserName());
        personInfoPO1.setName(personInfoPO.getName());
        personInfoPO1.setGender(personInfoPO.getGender());
        personInfoDAO.updateBaiscByPhone(personInfoPO1);
        return ResponseCode.REQUEST_SUCCESSED;

    }


    public int changeInfo(PersonInfo personInfo){
        PersonInfoPO personInfoPO = new PersonInfoPO();
        BeanUtils.copyProperties(personInfo,personInfoPO);
        UserPO userPO = userDAO.SelectByName(personInfoPO.getPhone());
        if(userPO == null){
            return ResponseCode.USER_NOT_EXISTS;
        }
        PersonInfoPO personSet = personInfoDAO.selectByPhone(personInfoPO.getPhone());
        if(!StringUtil.haveNullOrEmpty(personInfoPO.getName())){
            personSet.setName(personInfoPO.getName());
        }
        if(personInfoPO.getGender() != null){
            personSet.setGender(personInfoPO.getGender());
        }
        if(!StringUtil.haveNullOrEmpty(personInfoPO.getSynopsis())){
            personSet.setSynopsis(personInfoPO.getSynopsis());
        }
        if(!StringUtil.haveNullOrEmpty(personInfoPO.getCity())){
            personSet.setCity(personInfoPO.getCity());
        }
        personInfoDAO.updateInfoByPhone(personSet);
        return ResponseCode.REQUEST_SUCCESSED;
    }

}
